package com.hbx.controller;

import com.hbx.auth.AuthCacheService;
import com.hbx.constant.AuthContent;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.constant.RspEnum;
import com.hbx.dto.AdminLoginDto;
import com.hbx.exception.RspException;
import com.hbx.model.User;
import com.hbx.service.MenuService;
import com.hbx.service.UserService;
import com.hbx.utils.Result;
import com.hbx.vo.auth.AuthVo;
import com.hbx.vo.login.AuthResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/*
 * @Author:hbx
 * 
 * */
@Api(tags = "认证处理器")
@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    /**
     * 后台登录接口
     * @param user
     * @param result
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation("后台登录接口")
    public Result<AuthResultVo> login(@RequestBody @Valid @ApiParam(value = "登录信息",required = true) AdminLoginDto user, BindingResult result){

        Result<AuthResultVo> r = new Result<>();
        AuthResultVo vo = new AuthResultVo();
        if (result.hasErrors()){
            throw new RspException(ResponseStateConstant.PARAMETER_ERROR,result.getFieldError().getDefaultMessage());
        }

        User loginUser = null;

        Integer loginType = user.getLoginType();

        switch (loginType){
            case 1:
                loginUser = userService.usernameLogin(user);
                break;
            case 2:
                loginUser = userService.emailLogin(user);
                break;
            default:
                throw new RspException(ResponseStateConstant.OPERA_FAIL,"登录类型暂未授权");
        }

        if (ObjectUtils.isEmpty(loginUser)){
            throw new RspException(RspEnum.AUTH_LOGIN_ERROR);
        }

        userService.loginSuccess(loginUser,vo);
        r.setData(vo).setCode(ResponseStateConstant.OPERA_SUCCESS);
        return r;
    }

    /**
     * 获取用户信息
     * @return
     */
    @RequestMapping(value = "/getUserInfo",method = RequestMethod.POST)
    @ApiOperation(value = "获取用户信息")
    public Result getUserInfo(){
        Result<AuthVo> r = new Result<>();
        AuthVo authVo = new AuthVo();
        User user = AuthCacheService.getUser();
        if (ObjectUtils.isEmpty(user)){
            r.setCode(ResponseStateConstant.NO_LOGIN).setMsg("用户登录信息已过期");
            return r;
        }
        List<String> routeList = menuService.getMenuAuthByUserId(user.getUserId());
        authVo.setUser(user);
        authVo.setRoutes(routeList);
        r.setData(authVo).setCode(200);
        return r;
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value = "/logOut",method = RequestMethod.GET)
    @ApiOperation(value = "用户退出登录")
    public Result logOut(HttpServletRequest request){
        Result r = new Result<>();
        String token = request.getHeader(AuthContent.ACCESS_TOKEN);
        userService.logOut(token, r);
        return r;
    }
}
